package com.woniu.controller;

import com.woniu.entity.RbacRole;
import com.woniu.entity.ResponseEntity;
import com.woniu.service.RbacRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author WenXin
 * @version 1.0
 * @description: TODO
 * @date 2022/9/2 16:51
 */
@RestController
@RequestMapping("/rbacRole")
@CrossOrigin
public class RbacRoleController {
    @Autowired
//    @Qualifier("rbacRoleService")
//    @Resource //和Autowired明显区别是，优先通过byName去容器中找有没有这个bean，没有的话再用byType去找。
    RbacRoleService rbacRoleService;

    //主页初始数据渲染
    //角色权限为 role:findAll
//    @RequiresPermissions(logical = Logical.OR, value = {"role:findAll", "admin"})
    @GetMapping("/{pageNum}/{pageSize}")
    public ResponseEntity findAllLimit(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        rbacRoleService.findAll(pageNum, pageSize);
        return rbacRoleService.findAll(pageNum, pageSize);
    }

    @RequiresPermissions(logical = Logical.OR, value = {"role:list", "admin"})
    @GetMapping("/")
    public ResponseEntity findAll() {

        return rbacRoleService.findAll();
    }

    @PutMapping("/addOne")
//    @RequiresPermissions({"role:addOne", "admin"})
    public ResponseEntity addOne(@RequestBody RbacRole rbacRole) {
        int i = rbacRoleService.addOne(rbacRole);
        if (i > 0) {
            return new ResponseEntity(200, "新增成功", null);
        }
        return new ResponseEntity(500, "新增失败", null);
    }

    @DeleteMapping("/{id}")
//    @RequiresPermissions({"role:delete", "admin"})
    public ResponseEntity delete(@PathVariable int id) {
        int i = rbacRoleService.deleteById(id);
        if (i > 0) {
            return new ResponseEntity(200, "删除成功", null);
        }
        return new ResponseEntity(500, "删除失败", null);
    }

    @PutMapping("/")
    @RequiresPermissions({"role:update", "admin"})
    public ResponseEntity update(@RequestBody RbacRole rbacRole) {
        int update = rbacRoleService.update(rbacRole);
        if (update > 0) {
            return new ResponseEntity(200, "更新成功", null);
        }
        return new ResponseEntity(500, "更新失败", null);
    }

    //模糊搜索，含分页
    @PutMapping("/{pageNum}/{pageSize}")
    public ResponseEntity searchByRole(@RequestBody RbacRole rbacRole, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
     
        return rbacRoleService.searchByRole(rbacRole, pageNum, pageSize);
    }


}
